package com.tszs.biz;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tszs.dao.mapper.DistributorMapper;
import com.tszs.dao.mapper.PersonalOrderMapper;
import com.tszs.entity.DistributionAllocation;
import com.tszs.entity.DistributionAllocationExample;
import com.tszs.entity.DistributionAllocationExample.Criteria;
import com.tszs.entity.Distributor;
import com.tszs.entity.PersonalOrder;
import com.tszs.entity.PersonalOrderExample;
import com.tszs.util.DoubleUtil;

/**
 * @ 类名:PersonalOrderBiz
 * @ 类描述:会员订单
 * @ 创建时间: 2019-03-21 11:49:11
 * @author 王俊
 *
 */
@Service
public class PersonalOrderBiz {
	
	
	
	@Autowired
	private DistributorBiz distributorBiz;
	
	@Autowired
	private DistributionAllocationBiz distributionAllocationBiz;
	
	@Autowired
	private DistributorMapper distributorMapper;
	
	@Autowired
	private PersonalOrderMapper personalOrderMapper;
	/**
	 * @方法名: selectByExamplePage
	 * @方法描述: 订单查询分页查询
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 */
	@Transactional
	public List<PersonalOrder> selectByExamplePage(PersonalOrder example) {
		return personalOrderMapper.selectByExamplePage(example);
	}
	/**
	 * @方法名: selectByExample
	 * @方法描述: 订单根据ID查看详细信息
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 */
	@Transactional
	public List<PersonalOrder> selectByExample(PersonalOrderExample example) {
		return personalOrderMapper.selectByExample(example);
	}
	/**
	 * @方法名: insertSelective
	 * @方法描述: 订单添加
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	@Transactional
	public int insertSelective(PersonalOrder record) {
		return personalOrderMapper.insertSelective(record);
	}
	/**
	 * @方法名: updateByExampleSelective
	 * @方法描述: 订单修改
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	@Transactional
	public int updateByExampleSelective(@Param("record") PersonalOrder record, @Param("example") PersonalOrderExample example){
		return personalOrderMapper.updateByExampleSelective(record, example);
	}
	/**
	 * @方法名: OrderselectPage
	 * @方法描述: 订单报表
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	public List<PersonalOrder> OrderselectPage(PersonalOrder example) {
		return personalOrderMapper.OrderselectPage(example);
	}
	/**
	 * @方法名: 综合统计
	 * @方法描述: 订单报表
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	public List<PersonalOrder> findProductNum(PersonalOrder example) {
		return personalOrderMapper.findProductNum(example);
	}
	/**
	 * @方法名: findUidCar
	 * @方法描述: 个人购物车查询
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	public List<PersonalOrder> findUidCar(PersonalOrder example) {
		return personalOrderMapper.findUidCar(example);
	}
	/**
	 * @方法名: confirmationOfOrder
	 * @方法描述: 确认订单展示
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	public List<PersonalOrder> confirmationOfOrder(PersonalOrder example) {
		return personalOrderMapper.confirmationOfOrder(example);
	}
	/**
	 * @方法名: PersonalOrderExample
	 * @方法描述: 订单删除
	 * @创建时间: 2019-03-21 11:49:11
	 * @author: 王俊
	 * @return: 0失败 1成功
	 */
	public int deleteByExample(PersonalOrderExample example) {
		return personalOrderMapper.deleteByExample(example);
	}
	
	
	/***
	 * 订单支付完成修改状态
	 * @author MRC
	 * @date 2019年5月30日上午9:37:32
	 * @param personOrder
	 */
	public void updateOrderTradeSuccess(PersonalOrder personOrder){
		personalOrderMapper.updateOrderTradeSuccess(personOrder);
		
	}
	
	/**
	 * 订单数据分页
	 * @author Fujing
	 * @date 2019年5月31日 上午8:55:47
	 * @param example
	 * @return List<PersonalOrder>
	 */
	public PersonalOrder orderForPage(PersonalOrder example) {
		return personalOrderMapper.orderForPage(example);
	}
	
	/**
	 * 查询已评论
	 * @author Fujing
	 * @date 2019年6月1日 下午3:27:46
	 * @param example
	 * @return PersonalOrder
	 */
	public List<PersonalOrder> queryComment(PersonalOrder example) {
		return personalOrderMapper.queryComment(example);
	}
	
	
	/***
	 * 分销开始
	 * @author MRC
	 * @date 2019年5月31日上午9:46:31
	 * @param oId
	 */
	public void orderDistribution(PersonalOrder order) {
		
		String goodsId = order.getProductId();//商品id
		//查询是否有这个商品的分销配置存在
		DistributionAllocationExample example = new DistributionAllocationExample();
		Criteria criteria = example.createCriteria();
		criteria.andCIdEqualTo(goodsId);
		List<DistributionAllocation> selectByExample = distributionAllocationBiz.selectByExample(example);
		
		if (selectByExample.size() == 1) {
			//存在分销配置 开始分销
			Double totalPrice = Double.parseDouble(order.getTotalCommodityPrice());//订单总价
			Double cost =  Double.parseDouble(order.getuName());//订单成本
			Double profit = DoubleUtil.sub(totalPrice, cost);//利润
			
			DistributionAllocation allocation = selectByExample.get(0);//商品分销配置
			
			//取出分销商
			Distributor distributor = distributorBiz.selectByDidentification(order.getdDistributionidentification());
			for(int i =0;i<3;i++) {
				//最多分销三次
				//三级分销
				if (distributor.getdLevelIdentification().equals("3")) {
					distributorMapper.updateBalanceById(DoubleUtil.add(distributor.getdBalance(), DoubleUtil.mul(profit, Integer.valueOf(allocation.getThreeStageReturnCommission())*0.01)), distributor.getdId());
				}
				
				if (distributor.getdLevelIdentification().equals("2")) {
					distributorMapper.updateBalanceById(DoubleUtil.add(distributor.getdBalance(), DoubleUtil.mul(profit, Integer.valueOf(allocation.getTwoStageReturnCommission())*0.01)), distributor.getdId());
				}
	
				if (distributor.getdLevelIdentification().equals("1")) {
					distributorMapper.updateBalanceById(DoubleUtil.add(distributor.getdBalance(), DoubleUtil.mul(profit, Integer.valueOf(allocation.getFirstClassCommission())*0.01)), distributor.getdId());
				}
				
				if (StringUtils.isEmpty(distributor.getdSuperior())) {
					//不存在上级或者为空
					break;
				}
				distributor = distributorBiz.selectByDidentification(order.getdDistributionidentification());
				
				if (distributor == null) {
					break;
				}
				
			}
			
		}
		
		
	}
	
}
